using Habanero.BO;
using Habanero.UI.Base;
using Habanero.UI.Win;
using UseCaseManager.BO;

namespace UseCaseManager.UI
{
    /// <summary>
    /// A parent-child control used for managing a Release its Drops and the UseCases assigned to the Drop.
    /// </summary>
    public class ReleaseDropPlanningControl : UserControlWin, IFormControl
    {
        public ReleasesGridControl ReleaseSelector { private set; get; }

        public DropPlanningControl DropPlanningControl { get; private set; }

        private IControlFactory ControlFactory { get; set; }

        private BusinessObjectCollection<Release> _releasesCol;

        /// <summary>
        /// Constructs the <see cref="ReleaseDropPlanningControl"/>
        /// </summary>
        public ReleaseDropPlanningControl()
        {
            ControlFactory = GlobalUIRegistry.ControlFactory;
            ReleaseSelector = new ReleasesGridControl(new ControlFactoryWin()) {Height = 150};
            DropPlanningControl = new DropPlanningControl();

            BorderLayoutManager layoutManager = ControlFactory.CreateBorderLayoutManager(this);
            ReleaseSelector.Height = 150;
            layoutManager.AddControl(ReleaseSelector, BorderLayoutManager.Position.North);
            layoutManager.AddControl(DropPlanningControl, BorderLayoutManager.Position.Centre, true);

            // Connect the Release and Drops grids so that when an item is selected
            // in the Release grid, the correct Drops are shown in the child grid.
            ReleaseSelector.GridSelector.Grid.BusinessObjectSelected += 
                delegate
                {
                    Release selectedRelease = ReleaseSelector.GridSelector.Grid.SelectedBusinessObject as Release;
                    if (selectedRelease == null)
                    {
                        DropPlanningControl.DropCol = null;
                        DropPlanningControl.Release = null;
                        return;
                    }
                    DropPlanningControl.DropCol = selectedRelease.Drops;
                    DropPlanningControl.Release = selectedRelease;
                };
        }

        /// <summary>
        /// Returns the collection of drops that is being shown in the <see cref="ReleaseSelector"/>
        /// </summary>
        public BusinessObjectCollection<Release> ReleasesCol
        {
            get { return _releasesCol; }
            set
            {
                _releasesCol = value;
                ReleaseSelector.GridSelector.BusinessObjectCollection=_releasesCol;
                
            }
        }

        /// <summary>
        /// Called whenever the control is placed on a form by the menu system.  Put any initialisation
        /// code in here that must be run whenever the form is displayed.
        /// </summary>
        public void SetForm(IFormHabanero form)
        {
        }
    }
}